<template>
  <div class="cartcontrol">
    <transition name="move">
      <div class="trans-move-wrapper" v-show="food.count > 0">
        <div class="cart-decrease icon-remove_circle_outline"  @click.stop="descreaseCart($event)" ></div>
        <div class="cart-count" >{{food.count}}</div>
      </div>
    </transition>

    <div class="cart-add icon-add_circle" @click.stop="addCart($event)"></div>
  </div>
</template>

<script type="text/ecmascript-6">
import Vue from 'vue'
import bus from '../../assets/eventBus'

export default {
  name: 'cartcontrol',
  props: {
    food: {
      type: Object,
    },
  },
  methods: {
    addCart(event) {
      if (!event._constructed) {
        return
      }
      // if (!this.food.count) {
      //   // this.food.count = 1// vue对某个检测对象新增删除属性的时候，不能这样直接赋值，需要调用vue提供的接口，才能检测到
      //   Vue.set(this.food, 'count', 1)
      // } else {
      //   this.food.count++
      // }
      this.$store.commit('goods/addCart', this.food)
      // this.$emit('cart-add', event.target)
      this.sendAddCartEvent(event)
    },
    descreaseCart(event) {
      if (!event._constructed) {
        return
      }
      if (this.food.count > 0) {
        // this.food.count--
        this.$store.commit('goods/descreaseCart', this.food)
      }
    },
    sendAddCartEvent(event) {
      // console.log('sendAddCartEvent event')
      // console.log(event)
      bus.$emit('cartcontrol/addCart', event.target)
    },
  },
  created() {

  },
}
</script>

<style lang="stylus" rel="stylesheet/stylus">
  .cartcontrol
    font-size: 0
    .trans-move-wrapper
      display: inline-block
      &.move-enter-active
        transition: all 0.4s linear
      &.move-leave-active
        transition: all 0.4s linear
      &.move-enter, &.move-leave-to
        opacity: 0
        transform: translate3d(24px, 0, 0) // rotate(180deg)
      .cart-count
        display: inline-block
        vertical-align: top
        width: 12px
        padding-top: 6px
        line-height: 24px
        text-align: center
        font-size: 10px
        color: rgb(147, 153, 159)
      .cart-decrease
        display: inline-block
        vertical-align: top
        padding: 6px
        font-size: 24px
        line-height: 24px
        color: rgb(0, 160, 220)

    .cart-add
      color: rgb(0, 160, 220)
      display: inline-block
      vertical-align: top
      padding: 6px
      font-size: 24px
      line-height: 24px

</style>
